package cn.smile.dao.common;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import cn.smile.entity.common.Goods;
import cn.smile.entity.common.Student;

/**
 * 物品数据库操作dao
 */
@SuppressWarnings("all")
@Repository
public interface GoodsDao extends JpaRepository<Goods, Long>, JpaSpecificationExecutor<Goods> {
    /**
     * 根据id获取
     */
    @Query("select g from Goods g where id = :id")
    Goods find(@Param("id") Long id);

    /**
     * 根据学生查询
     */
    List<Goods> findByStudent(Student student);

    /**
     * 根据学生id和商品id查询
     */
    @Query("select g from Goods g where id = :id and g.student.id = :studentId")
    Goods find(@Param("id") Long id, @Param("studentId") Long studentId);

    /**
     * 根据物品分类查询物品列表
     */
    @Query(value = "SELECT * from ylrc_goods where goods_category_id IN :cids and `status` = 1 ORDER BY create_time desc,flag desc,recommend desc limit :offset,:pageSize", nativeQuery = true)
    List<Goods> findList(@Param("cids") List<Long> cids, @Param("offset") Integer offset, @Param("pageSize") Integer pageSize);

    /**
     * 获取根据分类搜索的总条数
     */
    @Query(value = "SELECT count(*) from ylrc_goods where goods_category_id IN :cids and `status` = 1 ", nativeQuery = true)
    Long getTotalCount(@Param("cids") List<Long> cids);

    /**
     * 获取指定状态的物品总数
     */
    @Query("SELECT count(g) from Goods g where g.status = :status ")
    Long getTotalCount(@Param("status") Integer status);

    /**
     * 根据物品名称模糊搜索
     */
    @Query(value = "select * from ylrc_goods where name like '%'+:name+'%'", nativeQuery = true)
    List<Goods> findListByName(@Param("name") String name);
}
